<?php
namespace app\farm\model;

use app\BaseModel;
use app\farm\model\BalanceWater;
use app\farm\model\User;
use app\shop\model\IntegralLog;
use think\facade\Db;

class BreedOrder extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_breed_order';


    protected $append = [

        'order_goods'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-01-10 16:04
     * @功能说明:
     */
    public function getOrderGoodsAttr($value,$data){

        if(!empty($data['id'])){

            $order_goods_model = new BreedOrderGoods();

            $list = $order_goods_model->where(['order_id'=>$data['id']])->select()->toArray();

            return $list;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-16 15:39
     * @功能说明:回调
     */
    public function orderResult($order_code,$transaction_id){

        $order = $this->dataInfo(['order_code'=>$order_code]);

        if(!empty($order)&&$order['pay_type']==1){

            $update = [

                'pay_time' => time(),

                'pay_type' => 7,

                'transaction_id' => $transaction_id
            ];

            $this->dataUpdate(['id'=>$order['id']],$update);
            //扣除余额
            if($order['balance']>0){

                $water_model = new BalanceWater();

                $res = $water_model->addWater($order,5,0);

                if($res==0){

                    Db::rollback();

                }
            }
            //添加流水
            $water_model = new FinanceWater();

            $water_model->addWater($order['id'],6,1,1);

            $integral_model = new IntegralLog();

            $integral_model->integralUserAdd($order['user_id'],$order['get_integral'],$order['uniacid'],2,7,$order['id']);


        }

        return true;

    }






}